//
//  SupplenessViewController.m
//  安好
//
//  Created by qinlongqing on 14/11/25.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SupplenessViewController.h"
#import "SupplenessTestingViewController.h"
#import "Reachability.h"
#import "CLDataBaseController.h"
#import "SupplenessTestResultViewController.h"

@interface SupplenessViewController ()
{
    CGFloat imageViewWidth;
    CGFloat imageViewHeight;
    CGFloat titleLabelHeight;
    CGFloat goOnBTNHeight;
}
@end

@implementation SupplenessViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self makeNavBar];
    
    [self.view setBackgroundColor:UIColorFromRGB(0x4fc1e9)];
    
    if (kScreenWidth == 414) {
        imageViewWidth = 312;
        imageViewHeight = 240;
        
        titleLabelHeight = 96;
        
        goOnBTNHeight = 54;
    }
    else{
        imageViewWidth = 260;
        imageViewHeight = 200;
        
        titleLabelHeight = 80;
        
        goOnBTNHeight = 45;
    }
    CGFloat space = (kScreenHeight - imageViewHeight - titleLabelHeight - goOnBTNHeight - 64)/4;
    
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake((kScreenWidth - imageViewWidth)/2, space, imageViewWidth, imageViewHeight);
    imageView.image = [UIImage imageNamed:@"rou_ren_xing_start"];
    [self.view addSubview:imageView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    if (kScreenWidth == 414) {
        titleLabel.frame = CGRectMake(20, imageView.frame.origin.y + imageView.frame.size.height +space, kScreenWidth - 40, titleLabelHeight);
        titleLabel.font = [UIFont systemFontOfSize:17];
    }
    else{
        titleLabel.frame = CGRectMake(15, imageView.frame.origin.y + imageView.frame.size.height +space, kScreenWidth - 30, titleLabelHeight);
        titleLabel.font = [UIFont systemFontOfSize:15];
    }
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"弯腰，将手机放置于双脚前，双手食指轻触屏幕，保持身体拉伸状态，坚持时间越久，得分越高";
    titleLabel.numberOfLines = 3;
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    titleLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:titleLabel];
    
    
    UIButton *goOnBTN = [[UIButton alloc]init];
    goOnBTN.layer.cornerRadius = 3;
    if (kScreenWidth == 414) {
        goOnBTN.frame = CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height + space, kScreenWidth - 40, goOnBTNHeight);
        goOnBTN.titleLabel.font = [UIFont systemFontOfSize:22];
    }
    else{
        goOnBTN.frame = CGRectMake(15, titleLabel.frame.origin.y + titleLabel.frame.size.height + space, kScreenWidth - 30, goOnBTNHeight);
        goOnBTN.titleLabel.font = [UIFont systemFontOfSize:20];
    }
    goOnBTN.backgroundColor = [UIColor whiteColor];
    [goOnBTN setTitleColor:UIColorFromRGB(0x4fc1e9) forState:UIControlStateNormal];
    [goOnBTN setTitle:@"开始测试" forState:UIControlStateNormal];
    [goOnBTN addTarget:self action:@selector(doGoOnBTN) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:goOnBTN];
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)makeNavBar{
    self.navigationItem.title = @"柔韧性";
    
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(doBack)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}

-(void)doBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)doGoOnBTN{
    SupplenessTestingViewController *supplenessTestingViewController = [[SupplenessTestingViewController alloc]init];
    supplenessTestingViewController.supplenessViewController = self;
    [self.navigationController pushViewController:supplenessTestingViewController animated:YES];
}

- (void)loadTestResult:(NSDictionary *)testResult{
    NSMutableDictionary *mTestResult = [testResult mutableCopy];
    
    //判断是否登录,如果已登录：直接存入服务器。如果未登录：存入本地
    if ([[NSString stringWithFormat:@"%@",[[UserManager shareInstance] getUserIsLogin]] length] != 0) {//已登录
        [self showActionView];
        NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion,@"type":@"11",@"daytime":[self setNewTime],@"flexility":[NSString stringWithFormat:@"%f",[[testResult valueForKey:kRouRenXingTestKeepedTime] floatValue]]};
        [[AFHTTPRequestOperationManager manager] POST:[NSString stringWithFormat:@"%@%@",kBaseURL,kUploadRouRenXingTestDataURL] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSNumber * statusNum = [responseObject objectForKey:@"status"];
            NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
            NSString * statusString = [numberFormatter stringFromNumber:statusNum];
            if ([statusString isEqualToString:@"0"]) {

            }else{
                NSString *userName = [[UserManager shareInstance] getUserInfo].username;
                [mTestResult setValue:userName forKey:kTestUserName];
                [self doShowTestResultView:mTestResult];
            }
            [self hiddenActionView];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self doShowTestResultView:mTestResult];
            [self hiddenActionView];
        }];
    }
    else{
        [self doShowTestResultView:mTestResult];
    }
}

//- (NSString *)setNewTime{
//    NSDate *now = [NSDate date];
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
//    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
//    int year = [dateComponent year];
//    int month = [dateComponent month];
//    int day = [dateComponent day];
//    
//    NSString *time = [NSString stringWithFormat:@"%d-%d-%d",year,month,day];
//    return time;
//}

- (NSString *)setNewTime{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *locationString=[formatter stringFromDate: [NSDate date]];
    return locationString;
    
}

- (void)doShowTestResultView:(NSDictionary *)dict{
    SupplenessTestResultViewController *supplenessTestResult = [[SupplenessTestResultViewController alloc]init];
    supplenessTestResult.testResultDict = dict;
    supplenessTestResult.supplenessViewController = self;
    [self.navigationController pushViewController:supplenessTestResult animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
